package cn.tedu.mall.order.controller;

import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import cn.tedu.mall.order.service.IOmsCartService;
import cn.tedu.mall.pojo.order.dto.CartAddDTO;
import cn.tedu.mall.pojo.order.dto.CartUpdateDTO;
import cn.tedu.mall.pojo.order.vo.CartStandardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/oms/cart")
@Api(tags = "购物车功能")
public class CartController {

    /**
     * 商品信息页面，提交新增购物车数据，存储到redis
     */

    @Autowired
    private IOmsCartService cartService;

    @PostMapping("/add")
    @PreAuthorize("hasRole('ROLE_user')")
    @ApiOperation(value = "前台将选择好的SKU添加到购物车")
    public JsonResult addCart(CartAddDTO cartAddDTO) {
        // 缺少userID，从jwt解析出来
        cartService.addCart(cartAddDTO);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @PreAuthorize("hasRole('ROLE_user')")
    @ApiOperation(value = "查询当前用户对应的所有购物车对象")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "pageNum", dataType = "int"),
            @ApiImplicitParam(value = "每页记录数", name = "pageSize", dataType = "int"),
    })
    public JsonResult<JsonPage<CartStandardVO>> listCarts(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        // 缺少userID，从jwt解析出来
        JsonPage<CartStandardVO> carts = cartService.listCarts(pageNum, pageSize);
        return JsonResult.ok(carts);
    }

    @PostMapping("/update/quantity")
    @ApiOperation(value = "购物车更新SKU商品的购买数量")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult<Void> updateQuantity(CartUpdateDTO cartUpdateDTO) {
        cartService.updateQuantity(cartUpdateDTO);
        return JsonResult.ok();
    }


}
